package com.uenpay.im.ui.old.activity.funds;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qiyunxin.android.http.utils.Utils;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.base.BaseActivity;

/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 * @E-mail:
 * @date 创建时间：2016-5-9 下午3:05:23
 * @version 1.0
 * @function 记一笔(添加经费支出)
 */
public class RecordFundsActivity extends BaseActivity {

	private Button comfirm_btn;
	private EditText cost_reason_et, cost_amount_et;
	private View loading;
	private String chatid;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		IMApplication.getInstance().addActivity(this);
		setContentView(R.layout.activity_add_funds_layout);
		chatid = getIntent().getStringExtra("chat_id");
		initView();
		initListener();
	}

	@Override
	protected void initView() {
		((TextView) findViewById(R.id.title_textview))
				.setText(R.string.add_funds);
		comfirm_btn = (Button) findViewById(R.id.comfirm_btn);
		cost_reason_et = (EditText) findViewById(R.id.cost_reason_et);
		cost_amount_et = (EditText) findViewById(R.id.cost_amount_et);
		loading = findViewById(R.id.loading);
	}

	@Override
	protected void initListener() {
		findViewById(R.id.back_layout).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						Utils.hideSoftKeyboard(cost_amount_et);
						Utils.hideSoftKeyboard(cost_reason_et);
						finish();
					}
				});
		cost_amount_et.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				checkBtnStatus();
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
			}

			@Override
			public void afterTextChanged(Editable arg0) {
			}
		});
		cost_reason_et.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				String content = cost_reason_et.getText().toString();
				if (Utils.String_length(content) > 30) {
					cost_reason_et.setText(Utils.getStringByLength(30, content));
					cost_reason_et.setSelection(cost_reason_et.getText().toString()
							.length());
					return;
				}
				checkBtnStatus();
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
			}

			@Override
			public void afterTextChanged(Editable arg0) {
			}
		});
		comfirm_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				String amount = cost_amount_et.getText().toString();
				String content = cost_reason_et.getText().toString();
				if (!TextUtils.isEmpty(amount) && !TextUtils.isEmpty(content)) {
					addFunds(amount, content);
				}
			}
		});
	}

	private void checkBtnStatus() {
		String reason = cost_reason_et.getText().toString();
		String amount = cost_amount_et.getText().toString();
		if (!TextUtils.isEmpty(reason) && !TextUtils.isEmpty(amount)) {
			comfirm_btn.setBackgroundResource(R.drawable.button_bg_normal);
			comfirm_btn.setEnabled(true);
		} else {
			comfirm_btn.setEnabled(false);
			comfirm_btn.setBackgroundResource(R.drawable.button_bg_gray);
		}
	}

	private void addFunds(String amount, String content) {
		loading.setVisibility(View.VISIBLE);
		FundsHandle.addFunds(chatid, "", amount, content, 2,
				new FundsHandle.IFundsListener() {

					@Override
					public void onFundsResult(int status, String msg) {
						loading.setVisibility(View.GONE);
						if (status == HttpInvokeResultCode.RESULT_OK) {
							setResult(RESULT_OK);
							finish();
						} else if (!TextUtils.isEmpty(msg)) {
							IMApplication.showToast(msg);
						}
					}
				});
	}

	@Override
	public void onBackPressed() {
		finish();
		super.onBackPressed();
	}

	@Override
	protected void onDestroy() {
		IMApplication.getInstance().removeActivity(this);
		super.onDestroy();
	}
}
